package com.mediacloud.app.newsmodule.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.model.news.LiveProgramListReciver;
import com.mediacloud.app.model.news.ProgramListItem;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.util.TimeUtils;

import java.util.List;

/**
 * Date:2021/7/21
 * Author:Wy
 */
public class ProgramDialog extends Dialog {

    private ImageView img_close;

    private RecyclerView recycler_left;

    private RecyclerView recycler_right;

    private LeftAdapter leftAdapter;

    private RightAdapter rightAdapter;

    //左边选中的位置
    private int leftPos = 0;
    //右边选中的位置
    private int rightPos = 0;
    //这个用来记录被选中右边时对应的左边item位置
    private int leftRightPos = 0;

    public OnProgramClick onProgramClick;

    /*public void setOnProgramClick(OnProgramClick listener) {
        if (onProgramClick == null) {
            onProgramClick = listener;
        }
    }*/

    public ProgramDialog(@NonNull Context context) {
        this(context, R.style.red_envelope_theme_dialog);
    }

    public ProgramDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ProgramDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_program_58holder);
        initView();
    }

    private void initView() {
        img_close = findViewById(R.id.img_close);
        recycler_left = findViewById(R.id.recycler_left);
        recycler_right = findViewById(R.id.recycler_right);
        img_close.setOnClickListener(v -> {
            dismiss();
        });
        leftAdapter = new LeftAdapter(R.layout.item_left);
        rightAdapter = new RightAdapter(R.layout.item_right);
        recycler_left.setLayoutManager(new LinearLayoutManager(getContext()));
        recycler_right.setLayoutManager(new LinearLayoutManager(getContext()));
        leftAdapter.bindToRecyclerView(recycler_left);
        rightAdapter.bindToRecyclerView(recycler_right);

        leftAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (position == leftPos) {
                return;
            }
            leftAdapter.getData().get(leftPos).selected = false;
            leftAdapter.getData().get(position).selected = true;
            leftAdapter.notifyDataSetChanged();
            leftPos = position;
            rightAdapter.setNewData(leftAdapter.getData().get(position).programList);
        });

        rightAdapter.setOnItemClickListener((adapter, view, position) -> {
            ProgramListItem item = (ProgramListItem) adapter.getItem(position);
            if (ProgramListItem.getItemState(item) == ProgramListItem.GuideItemState.COMING_SOON) {
                return;
            }
            if (onProgramClick != null) {
                //把正在播放的设置为非正在播放
                try{
                    leftAdapter.getData().get(leftRightPos).programList.get(rightPos).selected = false;
                    leftAdapter.getData().get(leftPos).programList.get(position).selected = true;
                }catch (Exception e){
                    e.printStackTrace();
                }

                rightPos = position;
                leftRightPos = leftPos;
                rightAdapter.notifyDataSetChanged();
                onProgramClick.click(item, leftPos, rightPos);
            }

        });
    }

    public void notifyProgramChanged(int pos) {
        if (rightAdapter == null || rightAdapter.getData().isEmpty()) {
            return;
        }
        rightAdapter.getData().get(rightPos).selected = false;
        rightAdapter.getData().get(pos).selected = true;
        rightPos = pos;
        rightAdapter.notifyDataSetChanged();
    }

    public boolean isAudio = false;

    public void setData(List<LiveProgramListReciver.ProgramWeekItem> data, int playPos) {
        rightAdapter.setAudio(isAudio);
        try{
        if (!data.isEmpty()) {
            leftPos=0;
            for (int i=0;i<data.size();i++){
                if (TimeUtils.isToday(data.get(i).getDate(), TimeUtils.YYYYMMDD_2_FORMAT)) {
                    leftPos=i;
                    break;
                }
            }
            data.get(leftPos).selected = true;
            data.get(leftPos).programList.get(playPos).selected = true;
            rightPos = playPos;
            if (rightPos > 4) {
                recycler_right.scrollToPosition(rightPos - 5);
            } else {
                recycler_right.scrollToPosition(rightPos);
            }
            rightAdapter.setNewData(data.get(leftPos).programList);
        }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public interface OnProgramClick {
        void click(ProgramListItem item, int leftPos, int rightPos);
    }

}
